package com.world.rickcloudendpoint.jobs;

import com.world.rickcloudendpoint.demo.DataTypeEnum;
import com.world.rickcloudendpoint.demo.QueueConstantKey;
import com.world.rickcloudendpoint.demo.model.DemoMessage;
import com.world.rickcloudendpoint.demo.redis.RedisQueueSender;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 定时任务
 * </p>
 *
 * @author yangkai.shen
 * @date Created in 2018-11-22 19:09
 */
@Component
@Slf4j
public class TaskJob {

    @Resource
    RedisQueueSender queueSender;
//    /**
//     * 按照标准时间来算，每隔 10s 执行一次
//     */
//    @Scheduled(cron = "0/10 * * * * ?")
//    public void job1() {
//        // 想netty服务端发送报文消息
////        log.info("【job1】开始执行：{}", DateUtil.formatDateTime(new Date()));
//    }
//
//    /**
//     * 从启动时间开始，间隔 2s 执行
//     * 固定间隔时间
//     */
//    @Scheduled(fixedRate = 2000)
//    public void job2() {
////        log.info("【job2】开始执行：{}", DateUtil.formatDateTime(new Date()));
//    }
//
//    /**
//     * 从启动时间开始，延迟 5s 后间隔 4s 执行
//     * 固定等待时间
//     */
//    @Scheduled(fixedDelay = 4000, initialDelay = 5000)
//    public void job3() {
////        log.info("【job3】开始执行：{}", DateUtil.formatDateTime(new Date()));
//    }

    private final String queuePrefix = QueueConstantKey.CACHE_PREFIX.concat(":");

    @Scheduled(fixedDelayString = "${job.producer.fixedDelay}", initialDelayString = "${job.producer.initialDelay}")
    public void oneProducer() {
        List<DemoMessage> test = mockData(DataTypeEnum.One);
        test.stream().forEach(r -> {
            queueSender.sendMsg(queuePrefix.concat(QueueConstantKey.RECEIVE_ONE), r);
        });
    }

    @Scheduled(fixedDelayString = "${job.producer.fixedDelay}", initialDelayString = "${job.producer.initialDelay}")
    public void twoProducer() {
        List<DemoMessage> test = mockData(DataTypeEnum.Two);
        test.stream().forEach(r -> {
            queueSender.sendMsg(queuePrefix.concat(QueueConstantKey.RECEIVE_TWO), r);
        });
    }

    @Scheduled(fixedDelayString = "${job.producer.fixedDelay}", initialDelayString = "${job.producer.initialDelay}")
    public void threeProducer() {
        List<DemoMessage> test = mockData(DataTypeEnum.Three);
        test.stream().forEach(r -> {
            queueSender.sendMsg(queuePrefix.concat(QueueConstantKey.RECEIVE_THREE), r);
        });
    }

    @Scheduled(fixedDelayString = "${job.producer.fixedDelay}", initialDelayString = "${job.producer.initialDelay}")
    public void fourProducer() {
        List<DemoMessage> test = mockData(DataTypeEnum.Four);
        test.stream().forEach(r -> {
            queueSender.sendMsg(queuePrefix.concat(QueueConstantKey.RECEIVE_FOUR), r);
        });
    }

    @Scheduled(fixedDelayString = "${job.producer.fixedDelay}", initialDelayString = "${job.producer.initialDelay}")
    public void fiveProducer() {
        List<DemoMessage> test = mockData(DataTypeEnum.Five);
        test.stream().forEach(r -> {
            queueSender.sendMsg(queuePrefix.concat(QueueConstantKey.RECEIVE_FIVE), r);
        });
    }

    @Scheduled(fixedDelayString = "${job.producer.fixedDelay}", initialDelayString = "${job.producer.initialDelay}")
    public void sixProducer() {
        List<DemoMessage> test = mockData(DataTypeEnum.Six);
        test.stream().forEach(r -> {
            queueSender.sendMsg(queuePrefix.concat(QueueConstantKey.RECEIVE_SIX), r);
        });
    }

    @Scheduled(fixedDelayString = "${job.producer.fixedDelay}", initialDelayString = "${job.producer.initialDelay}")
    public void sevenProducer() {
        List<DemoMessage> test = mockData(DataTypeEnum.Seven);
        test.stream().forEach(r -> {
            queueSender.sendMsg(queuePrefix.concat(QueueConstantKey.RECEIVE_SEVEN), r);
        });
    }

    @Scheduled(fixedDelayString = "${job.producer.fixedDelay}", initialDelayString = "${job.producer.initialDelay}")
    public void eightProducer() {
        List<DemoMessage> test = mockData(DataTypeEnum.Eight);
        test.stream().forEach(r -> {
            queueSender.sendMsg(queuePrefix.concat(QueueConstantKey.RECEIVE_EIGHT), r);
        });
    }

    public List<DemoMessage> mockData(DataTypeEnum dataType) {
        int stationNum = 8;
        int targetNum = 20;
        List<DemoMessage> test = new ArrayList<>();
        for (int i = 1; i <= stationNum; i++) {
            for (int j = 1; j <= targetNum; j++) {
                DemoMessage message = new DemoMessage();
                message.setTargetId(j + "");
                message.setStationId(i + "");
                message.setMsgType(dataType.getName());
                message.setName(i + ":" + j + ":" + dataType.getCode());
                message.setLatestTime(System.currentTimeMillis());
                message.setLon(117.273654);
                message.setLat(37.254868);
                message.setAlt(0.0);
                test.add(message);
            }
        }
        return test;
    }

}